package com.admin.utils;


import com.admin.business.config.oss.OSSUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Component
public class ImgImageBase64Util {


    public static String uploadImgBase64(String base64) throws IOException {
        try {
            MultipartFile file = OSSUtil.base64ToMultipart(base64);
            String contentType = file.getContentType();
            String fileName = file.getOriginalFilename();
            String newFileName = fileName;
            System.out.println("fileName-->" + newFileName);
            System.out.println("getContentType-->" + contentType);
            String filePath = "C:\\test/";
            System.out.println(filePath);
            String key = OSSUtil.uploadPic(file, newFileName);
            System.out.println(key + "---------");
            if (key == null) {
                return null;
            }
            return key;
        } catch (Exception e) {
            //return ResponseResult.build(400, "上传失败!", "");
        }
        return null;
    }

    public static String uploadImgFile(MultipartFile file,
                                       HttpServletRequest request) throws IOException {
        try {
            //MultipartFile file= OSSUtil.base64ToMultipart(base64);
            String contentType = file.getContentType();
            String fileName = file.getOriginalFilename();
            String newFileName = fileName;
            System.out.println("fileName-->" + newFileName);
            System.out.println("getContentType-->" + contentType);
            String filePath = "C:\\test/";
            System.out.println(filePath);
            String key = OSSUtil.uploadPic(file, newFileName);
            if (key == null) {
                return null;
            }
            return key;
        } catch (Exception e) {
            //return ResponseResult.build(400, "上传失败!", "");
        }
        return null;
    }

    public static String uploadFile(MultipartFile file,
                                    HttpServletRequest request) throws IOException {
        try {
            //MultipartFile file= OSSUtil.base64ToMultipart(base64);
            //String contentType = file.get;
            String fileName = file.getOriginalFilename();
            if (fileName.contains("vnd.openxmlformats-officedocument.wordprocessingml.document")) {//docs
                fileName = fileName.split("\\.vnd")[0] + ".docx";
            } else if (fileName.contains("vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {//xlsx
                fileName = fileName.split("\\.vnd")[0] + ".xlsx";
            } else if (fileName.contains("msword")) {//xlsx
                fileName = fileName.split("\\.msword")[0] + ".doc";
            } else if (fileName.contains("vnd.ms-excel")) {//xlsx
                fileName = fileName.split("\\.msword")[0] + ".xls";
            }
            System.out.println("fileName:" + fileName);
            String newFileName = fileName;
            //System.out.println("fileName-->" + newFileName);
            //System.out.println("getContentType-->" + contentType);
            String filePath = "C:\\test/";
            System.out.println(filePath);
            String key = OSSUtil.uploadFile(file, newFileName);
            if (key == null) {
                return null;
            }
            return key;
        } catch (Exception e) {
            //return ResponseResult.build(400, "上传失败!", "");
        }
        return null;
    }
}
